/**
 * Description:
 * Date: 2025-11-09
 * Time: 15:08
 */
public class MyLinkedList implements IList{

    static class ListNode {
        private int val;
        private ListNode prev;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    private ListNode head;
    private ListNode last;
    private int usedSize;

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null) {
            this.head = this.last = node;
            usedSize++;
            return;
        }
        node.next = this.head;
        this.head.prev = node;
        this.head = node;
        usedSize++;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null) {
            this.head = this.last = node;
            usedSize++;
            return;
        }
        this.last.next = node;
        node.prev = last;
        last = node;
        usedSize++;
    }

    @Override
    public void addIndex(int index, int data) {
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == this.usedSize) {
            addLast(data);
            return;
        }
        if (index < 0 || index > usedSize) {
            System.out.println("index出错！");
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
        usedSize++;

    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (this.head == null) {
            return;
        }
        if(this.head.val == key) {
            this.head = this.head.next;
            if(this.head == null) {
                usedSize--;
                return;
            }
            this.head.prev = null;
            usedSize--;
            return;
        }
        if(this.last.val == key) {
            this.last = this.last.prev;
            this.last.next = null;
            usedSize--;
            return;
        }
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                break;
            }
            cur  = cur.next;
        }
        if (cur == null) {
            return;
        }
        cur.next.prev = cur.prev;
        cur.prev.next = cur.next;
        usedSize--;
    }

    @Override
    public void removeAllKey(int key) {
        if (this.head == null) {
            return;
        }

        while (this.head != null && this.head.next != null && this.head.val == key) {
            this.head = this.head.next;
            this.head.prev = null;
            usedSize--;
        }
        if (this.head.next == null) {
            if (this.head.val == key){
                this.head = null;
            }
            return;
        }

        ListNode cur = this.head.next;
        while (cur != null) {
            if(cur.val == key) {
                /*if (cur.next == null) {
                    cur.prev.next = null;
                    usedSize--;
                    return;
                }*/
                if (cur == this.last) {
                    cur.prev.next = null;
                    usedSize--;
                    return;
                }
                cur.next.prev = cur.prev;
                cur.prev.next = cur.next;
                usedSize--;
            }
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        while (this.head != null && this.head.next != null) {
            this.head = this.head.next;
            this.head.prev = null;
        }
        this.head.prev = null;
        this.head = null;
        usedSize = 0;
    }




}
